package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.limao.CacheManager.CrowdPrestigeParmCache;
import com.limao.CacheManager.CrowdSmallCompanyCacheManager;
import com.limao.CacheManager.CrowdUserAuthCache;
import com.limao.CacheManager.SysGoodsCut_CacheManager;
import com.limao.CacheManager.VrGoodsCache;
import com.limao.CacheManager.user_parentCacheManager;
import com.limao.CacheManager.user_rmbCacheManager;
import com.limao.DBDataClass.CrowdPrestigeParmDataClass;
import com.limao.DBDataClass.CrowdSmallCompanyClass;
import com.limao.DBDataClass.CrowdSysProjectCutClass;
import com.limao.DBDataClass.CrowdUserAuthClass;
import com.limao.DBDataClass.CrowdUserParentClass;
import com.limao.DBDataClass.SysManDataClass;
import com.limao.DBDataClass.SysRmbDataClass;
import com.limao.DBDataClass.VrGoodsDataClass;
import com.limao.DBDataClass.userRmbDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Tool.CacheClass;
import com.limao.Tool.cutRmb;
import com.limao.push.SysRmbRecTypeEnum;
import com.limao.push.pushToAPPTemplate;


public class UserRmbProvider {

	
	private static class DB_UserRmbProviderContainer {
		private static UserRmbProvider instance = new UserRmbProvider();
	}

	public static UserRmbProvider getInstance() {
		return DB_UserRmbProviderContainer.instance;
	}

	// 定义系统表的name与操作语句
	private static final String sys_parent_table = "cd_sys_rmb";
	
	private static final String getSysMANList="{call getSysMANList(?,?,?)}"; //调用存储过程()
	private static final String Load_RankInfo="{call GetRankInfo(?,?)}"; //调用存储过程()
	
	private static final String INSERT_SYS_parent = "INSERT INTO "
			+ sys_parent_table + " (userid,payuserid,rmb,level,type,time,name,image,nowrmb,orderid) VALUES (?,?,?,?,?,?,?,?,?,?)";
	
	
	// 初始化
	private UserRmbProvider() {

	}
	
	
	//新鲜宝    查询用户收益参数值   by id
	public userRmbDataClass load_User_Rmb(int userid) {
		
		userRmbDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("SELECT * FROM cd_user_rmb WHERE userid=?");
			pstmt.setInt(1, userid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				
				tmp = new userRmbDataClass();
					
				tmp.userid= rs.getString("userid");
				tmp.principlelrmb= rs.getString("principlelrmb");
				tmp.nowrmb= rs.getString("nowrmb");
				tmp.totalbuyrmb= rs.getString("totalbuyrmb");
				tmp.totalbuyscore= rs.getString("totalbuyscore");
				tmp.totalwithinrmb= rs.getString("totalwithinrmb");
				tmp.total_mannum= rs.getString("total_mannum");
				
				tmp.totalinvestrmb= rs.getString("totalinvestrmb");
				
				tmp.vrnum= rs.getString("vrnum");
				tmp.redbagkey= rs.getString("redbagkey");
				tmp.rankprestigevalue= rs.getString("rankprestigevalue");
				tmp.prestigevalue= rs.getString("prestigevalue");
				tmp.virtualcoin= rs.getString("virtualcoin");
				tmp.virtualcoinice= rs.getString("virtualcoinice");
				tmp.maxvirtualcoin= rs.getString("maxvirtualcoin");

				
				tmp.rank= rs.getString("rank");

			}
		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	
	
	//团队人数加1
	public userRmbDataClass update_User_Mannum(int userid) {
		
		userRmbDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		userRmbDataClass tmp2=user_rmbCacheManager.getInstance().load_User_Rmb(userid);
		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("update cd_user_rmb set total_mannum=total_mannum+1 WHERE userid=?");
			pstmt.setInt(1, userid);
			if(pstmt.executeUpdate()==1)
			{
				tmp=tmp2;
				tmp.total_mannum=(CacheClass.getIntValue(tmp.total_mannum)+1)+"";
			}
	
		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	
	//&新鲜宝     取用户收益统计列表
	public PublicData get_Sys_RMB_List(int id,int curpage)
	{
		 Connection con = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;
	     PublicData tmp=null;
	     ArrayList<Object> listArry= null;
	     try {
	    	
	        List<SysRmbDataClass> authObjList= new ArrayList<SysRmbDataClass>();
	        int  total=0;

	        con = DbConnectionManager.getConnection();
    	    pstmt = con.prepareCall("{call getSysRMBForUserTypeList(?,?)}");  		 
            pstmt.setInt(1, id);
            pstmt.setInt(2, curpage);

            boolean hadResults=pstmt.execute();
            int mm=0;
            while (hadResults) {
            	if(tmp==null) tmp=new PublicData();
               	rs=pstmt.getResultSet();
               	if(mm==0)
               	{
               		while(rs != null && rs.next())
               		{
               			SysRmbDataClass Objtmp=new SysRmbDataClass();
 	               			Objtmp.id=rs.getString("id");
	               			Objtmp.userid=rs.getString("userid");
	               			Objtmp.payuserid=rs.getString("payuserid");
	               			Objtmp.rmb=rs.getString("rmb");
	               			Objtmp.level=rs.getString("level");
	               			Objtmp.type=rs.getString("type");
	               			Objtmp.time=rs.getString("time");

	               			Objtmp.timeAbout = CacheClass.getFormatDate(CacheClass.getLongValue(Objtmp.time),"yyyy年MM月dd日  HH:mm:ss");
	               			Objtmp.typename=SysRmbRecTypeEnum.getEnum(CacheClass.getIntValue(Objtmp.type)).getStringValue();
		               			
	               			Objtmp.image=rs.getString("image");
	               			Objtmp.name=rs.getString("name");
	               			if(CacheClass.isEmpty(Objtmp.image))
	               				Objtmp.image="userimage.jpg";
	               			if(CacheClass.isEmpty(Objtmp.name))
	               				Objtmp.name="系统";
	               			Objtmp.nowrmb=rs.getString("nowrmb");;
	               			authObjList.add(Objtmp);
               		}
               		
               	}
              	else if(mm==1)
               	{
               		while(rs != null && rs.next()){
               			total=total+rs.getInt(1);
               		}
               	}
                hadResults = pstmt.getMoreResults(); //检查是否存在更多结果集
                mm++;
            }
 	        

 	       if(authObjList.size()>0)
 	       {
 	    	   if(listArry==null) listArry=new ArrayList<Object>();
 	    	   listArry.addAll(authObjList);
 	       }
 	       
			if((total%20)>0)
			{
				total=(total/20+1);
			}
			else
				total=(total/20);
			
			if(total==0) total=1;
			tmp.total=total+"";

            tmp.curpage=curpage+"";
            tmp.list=listArry;



       }
       catch (Exception e) {
       		tmp=null;
           e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	    return tmp;

	}	
	
	
	
	
	
	//新鲜宝    取账单列表
	public PublicData get_Sys_RMB_List3(int id,int curpage)
	{
		 Connection con = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;
	     PublicData tmp=null;
	     ArrayList<Object> listArry= null;
	     try {
	    	
	        List<SysRmbDataClass> authObjList= new ArrayList<SysRmbDataClass>();
	        int  total=0;

	        con = DbConnectionManager.getConnection();
	        pstmt = con.prepareCall("{call getSysRMBForList(?,?)}");  		 
            pstmt.setInt(1, id);
            pstmt.setInt(2, curpage);

            boolean hadResults=pstmt.execute();
            int mm=0;

            while (hadResults) {
            	if(tmp==null) tmp=new PublicData();
               	rs=pstmt.getResultSet();
               	if(mm==0)
               	{
               		while(rs != null && rs.next())
               		{
               			SysRmbDataClass Objtmp=new SysRmbDataClass();
 	               			Objtmp.id=rs.getString("id");
	               			Objtmp.userid=rs.getString("userid");
	               			Objtmp.payuserid=rs.getString("payuserid");
	               			Objtmp.rmb=rs.getString("rmb");
	               			Objtmp.level=rs.getString("level");
	               			Objtmp.type=rs.getString("type");
	               			Objtmp.typename=SysRmbRecTypeEnum.getEnum(CacheClass.getIntValue(Objtmp.type)).getStringValue();
	               			Objtmp.time=rs.getString("time");
	               			Objtmp.image=rs.getString("image");
	               			Objtmp.name=rs.getString("name");
	               			if(CacheClass.isEmpty(Objtmp.image))
	               				Objtmp.image="userimage.jpg";
	               			if(CacheClass.isEmpty(Objtmp.name))
	               				Objtmp.name="系统";
               				authObjList.add(Objtmp);
               		}
               		
               	}
              	else if(mm==1)
               	{
               		while(rs != null && rs.next()){
               			total=total+rs.getInt(1);
               		}
               	}
              	
                hadResults = pstmt.getMoreResults(); //检查是否存在更多结果集
                mm++;
            }
            DbConnectionManager.closeConnection(rs, pstmt, con);	        

 	       if(authObjList.size()>0)
 	       {
 	    	   if(listArry==null) listArry=new ArrayList<Object>();
 	    	   listArry.addAll(authObjList);
 	       }
 	       
			if((total%20)>0)
			{
				total=(total/20+1);
			}
			else
				total=(total/20);
			
			if(total==0) total=1;
			tmp.total=total+"";
   			
            tmp.curpage=curpage+"";
            tmp.list=listArry;

       }
       catch (Exception e) {
       		tmp=null;
           e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	    return tmp;

	}		
		
	
	//&新鲜宝     取用户现货瓶数记录列表
	public PublicData get_Sys_RMB_List4(int id,int curpage)
	{
		 Connection con = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;
	     PublicData tmp=null;
	     ArrayList<Object> listArry= null;
	     try {
	    	
	        List<SysRmbDataClass> authObjList= new ArrayList<SysRmbDataClass>();
	        int  total=0;

	        con = DbConnectionManager.getConnection();
    	    pstmt = con.prepareCall("{call getSysMaForUserTypeList2(?,?)}");  		 
            pstmt.setInt(1, id);
            pstmt.setInt(2, curpage);

            boolean hadResults=pstmt.execute();
            int mm=0;
            while (hadResults) {
            	if(tmp==null) tmp=new PublicData();
               	rs=pstmt.getResultSet();
               	if(mm==0)
               	{
               		while(rs != null && rs.next())
               		{
               			SysRmbDataClass Objtmp=new SysRmbDataClass();
 	               			Objtmp.id=rs.getString("id");
	               			Objtmp.userid=rs.getString("userid");
	               			Objtmp.payuserid=rs.getString("payuserid");
	               			Objtmp.rmb=rs.getString("rmb");
	               			Objtmp.level=rs.getString("level");
	               			Objtmp.type=rs.getString("type");
	               			Objtmp.time=rs.getString("time");

	               			Objtmp.timeAbout = CacheClass.getFormatDate(CacheClass.getLongValue(Objtmp.time),"yyyy年MM月dd日  HH:mm:ss");
	               			Objtmp.typename=SysRmbRecTypeEnum.getEnum(CacheClass.getIntValue(Objtmp.type)).getStringValue();
		               			
	               			Objtmp.image=rs.getString("image");
	               			Objtmp.name=rs.getString("name");
	               			if(CacheClass.isEmpty(Objtmp.image))
	               				Objtmp.image="userimage.jpg";
	               			if(CacheClass.isEmpty(Objtmp.name))
	               				Objtmp.name="系统";
	               			Objtmp.nowrmb=rs.getString("nowrmb");;
	               			authObjList.add(Objtmp);
               		}
               		
               	}
              	else if(mm==1)
               	{
               		while(rs != null && rs.next()){
               			total=total+rs.getInt(1);
               		}
               	}
                hadResults = pstmt.getMoreResults(); //检查是否存在更多结果集
                mm++;
            }
 	        

 	       if(authObjList.size()>0)
 	       {
 	    	   if(listArry==null) listArry=new ArrayList<Object>();
 	    	   listArry.addAll(authObjList);
 	       }
 	       
			if((total%20)>0)
			{
				total=(total/20+1);
			}
			else
				total=(total/20);
			
			if(total==0) total=1;
			tmp.total=total+"";

            tmp.curpage=curpage+"";
            tmp.list=listArry;



       }
       catch (Exception e) {
       		tmp=null;
           e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	    return tmp;

	}		
		
	
	
	
	//&新鲜宝 取用户团队列表
	public PublicData get_Sys_MANS_List(int id,String keyword,int curpage)
	{
		 Connection con = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;
	     PublicData tmp=null;
	     ArrayList<Object> listArry= null;
	     try {
	    	
	        List<SysManDataClass> authObjList= new ArrayList<SysManDataClass>();
	        int  total=0;

	        con = DbConnectionManager.getConnection();
    	    pstmt = con.prepareCall(getSysMANList);  	
            pstmt.setInt(1, id);
            pstmt.setString(2, keyword);
            pstmt.setInt(3, curpage);

            int totalman=0;
            boolean hadResults=pstmt.execute();
            int mm=0;
            while (hadResults) {
            	if(tmp==null) tmp=new PublicData();
               	rs=pstmt.getResultSet();
               	if(mm==0)
               	{
               		while(rs != null && rs.next())
               		{
               			SysManDataClass Objtmp=new SysManDataClass();
 	               			Objtmp.userid=rs.getString("userid");
	               			Objtmp.mobile=rs.getString("mobile");
	               			
	               			int level=rs.getInt("level");
	               			if(level==1)
	               			{
	               				Objtmp.level="B级分销商";
	               			}
	               			else
	               			{
	               				Objtmp.level="C级分销商";
	               			}
	               			
	               			
	               			Objtmp.time=rs.getString("time");
	               			Objtmp.username=rs.getString("username");
	               			Objtmp.avatar=rs.getString("avatar");
            			
	               			authObjList.add(Objtmp);
               		}
               		
               	}
               	else if(mm==1)
               	{
               		while(rs != null && rs.next()){
               			total=rs.getInt(1);
               		}
               	}
             	else
             	{
             		if(rs != null && rs.next())
             		{
	             		int man1=rs.getInt("man1");
	             		int man2=rs.getInt("man2");
	             		tmp.totalman=(man1+man2)+"";
	             		tmp.totalman1=man1+"";
	             		tmp.totalman2=man2+"";
             		}
             		else
             		{
             			tmp.totalman="0";
	             		tmp.totalman1="0";
	             		tmp.totalman2="0";
             		}
             	}

                hadResults = pstmt.getMoreResults(); //检查是否存在更多结果集
                mm++;
            }
            DbConnectionManager.closeConnection(rs, pstmt, con);	        

 	       if(authObjList.size()>0)
 	       {
 	    	   if(listArry==null) listArry=new ArrayList<Object>();
 	    	   listArry.addAll(authObjList);
 	       }
 	       
			if((total%20)>0)
			{
				total=(total/20+1);
			}
			else
				total=(total/20);
			
			if(total==0) total=1;
			tmp.total=total+"";

            tmp.curpage=curpage+"";
            tmp.totalman=totalman+"";
            tmp.list=listArry;

       }
       catch (Exception e) {
       		tmp=null;
           e.printStackTrace();
	   	} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}


	    return tmp;

	}	
		
	
	
	
		
/********************************************************************************************************************/


	
	
/*******************************************************************************************************************************/
	
	
	//&新鲜宝  购买 分成(支付成功后  进行购买分成)
	public CrowdUserAuthClass BuyCut(float totalRmb,int goodsnum, int userid,int userViplevel,String orderid,String projectid,String projectname)
	{
		
		CrowdUserParentClass tmp=user_parentCacheManager.getInstance().load_user_Parent(userid);
		if(tmp==null) return null;
		
		//获取商品分成参数
		CrowdSysProjectCutClass goodscut=SysGoodsCut_CacheManager.getInstance().loadGoodsCut(projectid);
		
		CrowdUserAuthClass authTmp=null;
		
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Date now =new Date();
		long time=now.getTime();
		int level=0;
		int viplevel=0;
		float rmb=0; //现金
		float rmbForNum=0; //瓶数的金额
		int type=SysRmbRecTypeEnum.type8.getIntVlue(); //1:购买分成积分
		authTmp=CrowdUserAuthCache.getInstance().getUser(userid);
		String payUsername=authTmp.username;
		
		int vrgoodsid=0;
		VrGoodsDataClass vrgoods=VrGoodsCache.getInstance().Load_VrGoods_ByListFirst();
		if(vrgoods!=null)
		{
			vrgoodsid=CacheClass.getIntValue(vrgoods.vrgoodsid);
		}
		//取当前现货价格
		float vrgoodsprice=CacheClass.getFloatValue(vrgoods.price);
		
			try {
				con = DbConnectionManager.getConnection();
				//现金分成 与 瓶数分成
				if(goodscut!=null && CacheClass.getIntValue(goodscut.regular)!=0)   //有分成设置并且分成启用
				{
					
					//增加本人的正常购物分成积分
					int yesornot=CacheClass.getIntValue(goodscut.buy_self_ornot.trim());
					if(yesornot==1)//是否计算触发人(即本级)的邀请积分。　1计算，0不计算
					{
						//先本人自己增加
						viplevel=userViplevel;
						if(userid>0){	
							level=0;
							//现金分成
							rmb=cutRmb.getBuyCutRmb(totalRmb,goodsnum,level,viplevel,goodscut);
							if(rmb>0)
							{
								//增加金额
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("update cd_user_rmb set nowrmb =(nowrmb+?),totalbuyrmb=(totalbuyrmb+?) where userid=?");
								pstmt.setFloat(1, rmb);
								pstmt.setFloat(2, rmb);
								pstmt.setInt(3, userid);
								pstmt.executeUpdate();
								
								//取更新后的新值
								String nowrmb="0";
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("select nowrmb from cd_user_rmb where userid=?");
								pstmt.setInt(1, userid);
								rs=pstmt.executeQuery();
								if(rs.next())
								{
									nowrmb=rs.getString("nowrmb");
								}
								
			                    //记录
								pstmt = con.prepareStatement(INSERT_SYS_parent);
								pstmt.setInt(1, userid);
								pstmt.setInt(2, userid);
								pstmt.setFloat(3, rmb); 
								pstmt.setInt(4, level);
								pstmt.setInt(5, type);
								pstmt.setLong(6, time);
								pstmt.setString(7, authTmp.username);
								pstmt.setString(8, authTmp.avatar);
								pstmt.setBigDecimal(9, CacheClass.toBigDecimal(nowrmb));
								pstmt.setLong(10, CacheClass.getLongValue(orderid));
								pstmt.execute();	
								//触发，系统消息发给收获人
								pushToAPPTemplate.NewMess_BuyCutRmb(userid, level, payUsername, projectname, rmb);
								//更新缓存
								CrowdUserAuthCache.getInstance().ClearObjectCache(userid+"");
							}
							
							//瓶数分成
							rmbForNum=cutRmb.getBuyCutNum(totalRmb,goodsnum,level,viplevel,goodscut);
							//送现货手瓶数
							float vrgoodsnum=rmbForNum/(vrgoodsprice);
							if(vrgoodsnum>0 && vrgoodsid>0)
							{
								//现货品种瓶数更新
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("select * from cd_user_num where userid=? and vrgoodsid=? ;");
								pstmt.setInt(1, userid);
								pstmt.setInt(2, vrgoodsid);
								rs=pstmt.executeQuery();
								if(rs.next()) //存在 则更新
								{
									DbConnectionManager.fastcloseStmt(rs,pstmt);
									pstmt = con.prepareStatement("update cd_user_num set num=(num+?) where userid=? and vrgoodsid=? ;");
									pstmt.setBigDecimal(1, CacheClass.toBigDecimal(vrgoodsnum+""));
									pstmt.setInt(2, userid);
									pstmt.setInt(3, vrgoodsid);
									pstmt.executeUpdate();
									
								}
								else//不存在  则插入
								{
									DbConnectionManager.fastcloseStmt(rs,pstmt);
									pstmt = con.prepareStatement("insert into cd_user_num (userid,vrgoodsid,num) VALUES (?,?,?);");
									pstmt.setInt(1, userid);
									pstmt.setInt(2, vrgoodsid);
									pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
									pstmt.execute();
									
								}
								
								//取更新后的新值
								String manowtotalrmb="0";
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("select num from cd_user_num where userid=? and vrgoodsid=? ;");
								pstmt.setInt(1, userid);
								pstmt.setInt(2, vrgoodsid);
								rs=pstmt.executeQuery();
								if(rs.next())
								{
									manowtotalrmb=rs.getString("num");
								}
								
								//增加现货瓶数变动记录
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("INSERT INTO cd_sys_rmb (userid,payuserid,rmb,level,type,time,name,image,nowrmb,orderid,vrgoodsid) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
								pstmt.setInt(1, userid);
								pstmt.setInt(2, userid);
								pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.setInt(4, level);
								pstmt.setInt(5, SysRmbRecTypeEnum.type109.getIntVlue());
								pstmt.setLong(6, now.getTime());
								pstmt.setString(7, authTmp.username);
								pstmt.setString(8, authTmp.avatar);
								pstmt.setBigDecimal(9, CacheClass.toBigDecimal(manowtotalrmb));
								pstmt.setLong(10, CacheClass.getLongValue(orderid));
								pstmt.setInt(11, vrgoodsid);
								pstmt.execute();
								//触发，系统消息发给收获人
								pushToAPPTemplate.NewMess_BuyCutNum(userid, level, payUsername, projectname, vrgoodsnum);
								//更新缓存
								CrowdUserAuthCache.getInstance().ClearObjectCache(userid+"");
							}
							
						}	
					}
					
					//增加上一级的正常购物分成积分
					if(CacheClass.getIntValue(tmp.pid0)>0 ){
					//if(CacheClass.getIntValue(tmp.pid0)>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid0))){
						CrowdUserAuthClass authCut=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid0));
						viplevel=CacheClass.getIntValue(authCut.viplevel);
						level=1;
						rmb=cutRmb.getBuyCutRmb(totalRmb,goodsnum,level,viplevel,goodscut);
						//现金分成
						if(rmb>0)
						{
							//增加金额
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("update cd_user_rmb set nowrmb =(nowrmb +?),totalbuyrmb=(totalbuyrmb+?) where userid=?");
							pstmt.setFloat(1, rmb);
							pstmt.setFloat(2, rmb);
							pstmt.setInt(3, CacheClass.getIntValue(tmp.pid0));
							pstmt.executeUpdate();
							
							
							//取更新后的新值
							String nowrmb="0";
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select nowrmb from cd_user_rmb where userid=?");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
							rs=pstmt.executeQuery();
							if(rs.next())
							{
								nowrmb=rs.getString("nowrmb");
							}
							
		                    //记录
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement(INSERT_SYS_parent);
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
							pstmt.setInt(2, userid);
							pstmt.setFloat(3, rmb);
							pstmt.setInt(4, level);
							pstmt.setInt(5, type);
							pstmt.setLong(6, time);
							pstmt.setString(7, authTmp.username);
							pstmt.setString(8, authTmp.avatar);
							pstmt.setBigDecimal(9, CacheClass.toBigDecimal(nowrmb));
							pstmt.setLong(10, CacheClass.getLongValue(orderid));
							pstmt.execute();	

							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutRmb(CacheClass.getIntValue(tmp.pid0), level, payUsername, projectname, rmb);	
							
							//更新缓存
							CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid0);
						}
						
						//瓶数分成
						rmbForNum=cutRmb.getBuyCutNum(totalRmb,goodsnum,level,viplevel,goodscut);
						//送现货手瓶数
						float vrgoodsnum=rmbForNum/(vrgoodsprice);
						if(vrgoodsnum>0 && vrgoodsid>0)
						{
							//现货品种瓶数更新
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select * from cd_user_num where userid=? and vrgoodsid=? ;");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
							pstmt.setInt(2, vrgoodsid);
							rs=pstmt.executeQuery();
							if(rs.next()) //存在 则更新
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("update cd_user_num set num=(num+?) where userid=? and vrgoodsid=? ;");
								pstmt.setBigDecimal(1, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.setInt(2, CacheClass.getIntValue(tmp.pid0));
								pstmt.setInt(3, vrgoodsid);
								pstmt.executeUpdate();
								
							}
							else//不存在  则插入
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("insert into cd_user_num (userid,vrgoodsid,num) VALUES (?,?,?);");
								pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
								pstmt.setInt(2, vrgoodsid);
								pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.execute();
								
							}
							
							//取更新后的新值
							String manowtotalrmb="0";
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select num from cd_user_num where userid=? and vrgoodsid=? ;");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
							pstmt.setInt(2, vrgoodsid);
							rs=pstmt.executeQuery();
							if(rs.next())
							{
								manowtotalrmb=rs.getString("num");
							}
							
							//增加现货瓶数变动记录
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("INSERT INTO cd_sys_rmb (userid,payuserid,rmb,level,type,time,name,image,nowrmb,orderid,vrgoodsid) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
							pstmt.setInt(2, userid);
							pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
							pstmt.setInt(4, level);
							pstmt.setInt(5, SysRmbRecTypeEnum.type109.getIntVlue());
							pstmt.setLong(6, now.getTime());
							pstmt.setString(7, authTmp.username);
							pstmt.setString(8, authTmp.avatar);
							pstmt.setBigDecimal(9, CacheClass.toBigDecimal(manowtotalrmb));
							pstmt.setLong(10, CacheClass.getLongValue(orderid));
							pstmt.setInt(11, vrgoodsid);
							pstmt.execute();
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutNum(CacheClass.getIntValue(tmp.pid0), level, payUsername, projectname, vrgoodsnum);
							//更新缓存
							CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid0);
						}
						
					}
					//增加上二级的正常购物分成积分
					if(CacheClass.getIntValue(tmp.pid1)>0 ){
					//if(CacheClass.getIntValue(tmp.pid1)>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid1))){
						CrowdUserAuthClass authCut=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid1));
						viplevel=CacheClass.getIntValue(authCut.viplevel);
						level=2;
						rmb=cutRmb.getBuyCutRmb(totalRmb,goodsnum,level,viplevel,goodscut);
						//现金分成
						if(rmb>0)
						{
							//增加金额
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("update cd_user_rmb set nowrmb =(nowrmb +?),totalbuyrmb=(totalbuyrmb+?) where userid=?");
							pstmt.setFloat(1, rmb);
							pstmt.setFloat(2, rmb);
							pstmt.setInt(3, CacheClass.getIntValue(tmp.pid1));
							pstmt.executeUpdate();
							
							//取更新后的新值
							String nowrmb="0";
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select nowrmb from cd_user_rmb where userid=?");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
							rs=pstmt.executeQuery();
							if(rs.next())
							{
								nowrmb=rs.getString("nowrmb");
							}
							
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement(INSERT_SYS_parent);
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
							pstmt.setInt(2, userid);
							pstmt.setFloat(3, rmb);
							pstmt.setInt(4, level);
							pstmt.setInt(5, type);
							pstmt.setLong(6, time);
							pstmt.setString(7, authTmp.username);
							pstmt.setString(8, authTmp.avatar);
							pstmt.setBigDecimal(9, CacheClass.toBigDecimal(nowrmb));
							pstmt.setLong(10, CacheClass.getLongValue(orderid));
							pstmt.execute();			
		
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutRmb(CacheClass.getIntValue(tmp.pid1), level, payUsername, projectname,rmb);
							//更新缓存
							CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid1);
						}
						
						//瓶数分成
						rmbForNum=cutRmb.getBuyCutNum(totalRmb,goodsnum,level,viplevel,goodscut);
						//送现货手瓶数
						float vrgoodsnum=rmbForNum/(vrgoodsprice);
						if(vrgoodsnum>0 && vrgoodsid>0)
						{
							//现货品种瓶数更新
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select * from cd_user_num where userid=? and vrgoodsid=? ;");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
							pstmt.setInt(2, vrgoodsid);
							rs=pstmt.executeQuery();
							if(rs.next()) //存在 则更新
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("update cd_user_num set num=(num+?) where userid=? and vrgoodsid=? ;");
								pstmt.setBigDecimal(1, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.setInt(2, CacheClass.getIntValue(tmp.pid1));
								pstmt.setInt(3, vrgoodsid);
								pstmt.executeUpdate();
								
							}
							else//不存在  则插入
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("insert into cd_user_num (userid,vrgoodsid,num) VALUES (?,?,?);");
								pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
								pstmt.setInt(2, vrgoodsid);
								pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.execute();
								
							}
							
							//取更新后的新值
							String manowtotalrmb="0";
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select num from cd_user_num where userid=? and vrgoodsid=? ;");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
							pstmt.setInt(2, vrgoodsid);
							rs=pstmt.executeQuery();
							if(rs.next())
							{
								manowtotalrmb=rs.getString("num");
							}
							
							//增加现货瓶数变动记录
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("INSERT INTO cd_sys_rmb (userid,payuserid,rmb,level,type,time,name,image,nowrmb,orderid,vrgoodsid) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
							pstmt.setInt(2, userid);
							pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
							pstmt.setInt(4, level);
							pstmt.setInt(5, SysRmbRecTypeEnum.type109.getIntVlue());
							pstmt.setLong(6, now.getTime());
							pstmt.setString(7, authTmp.username);
							pstmt.setString(8, authTmp.avatar);
							pstmt.setBigDecimal(9, CacheClass.toBigDecimal(manowtotalrmb));
							pstmt.setLong(10, CacheClass.getLongValue(orderid));
							pstmt.setInt(11, vrgoodsid);
							pstmt.execute();
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutNum(CacheClass.getIntValue(tmp.pid1), level, payUsername, projectname, vrgoodsnum);
							//更新缓存
							CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid1);
						}
						
						
					}
					
					//增加上三级的正常购物分成积分
					if(CacheClass.getIntValue(tmp.pid2)>0 ){
					//if(CacheClass.getIntValue(tmp.pid2)>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid2))){
						CrowdUserAuthClass authCut=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid2));
						viplevel=CacheClass.getIntValue(authCut.viplevel);
						level=3;
						rmb=cutRmb.getBuyCutRmb(totalRmb,goodsnum,level,viplevel,goodscut);
						//现金分成
						if(rmb>0)
						{
							//增加金额
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("update cd_user_rmb set nowrmb =(nowrmb +?),totalbuyrmb=(totalbuyrmb+?) where userid=?");
							pstmt.setFloat(1, rmb);
							pstmt.setFloat(2, rmb);
							pstmt.setInt(3, CacheClass.getIntValue(tmp.pid2));
							pstmt.executeUpdate();
							
							//取更新后的新值
							String nowrmb="0";
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select nowrmb from cd_user_rmb where userid=?");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
							rs=pstmt.executeQuery();
							if(rs.next())
							{
								nowrmb=rs.getString("nowrmb");
							}
							
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement(INSERT_SYS_parent);
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
							pstmt.setInt(2, userid);
							pstmt.setFloat(3, rmb);
							pstmt.setInt(4, level);
							pstmt.setInt(5, type);
							pstmt.setLong(6, time);
							pstmt.setString(7, authTmp.username);
							pstmt.setString(8, authTmp.avatar);
							pstmt.setBigDecimal(9, CacheClass.toBigDecimal(nowrmb));
							pstmt.setLong(10, CacheClass.getLongValue(orderid));
							pstmt.execute();			

							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutRmb(CacheClass.getIntValue(tmp.pid2), level, payUsername, projectname,rmb);
							//更新缓存
							CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid2);
						}
						
						//瓶数分成
						rmbForNum=cutRmb.getBuyCutNum(totalRmb,goodsnum,level,viplevel,goodscut);
						//送现货手瓶数
						float vrgoodsnum=rmbForNum/(vrgoodsprice);
						if(vrgoodsnum>0 && vrgoodsid>0)
						{
							//现货品种瓶数更新
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select * from cd_user_num where userid=? and vrgoodsid=? ;");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
							pstmt.setInt(2, vrgoodsid);
							rs=pstmt.executeQuery();
							if(rs.next()) //存在 则更新
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("update cd_user_num set num=(num+?) where userid=? and vrgoodsid=? ;");
								pstmt.setBigDecimal(1, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.setInt(2, CacheClass.getIntValue(tmp.pid2));
								pstmt.setInt(3, vrgoodsid);
								pstmt.executeUpdate();
								
							}
							else//不存在  则插入
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("insert into cd_user_num (userid,vrgoodsid,num) VALUES (?,?,?);");
								pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
								pstmt.setInt(2, vrgoodsid);
								pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
								pstmt.execute();
								
							}
							
							//取更新后的新值
							String manowtotalrmb="0";
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("select num from cd_user_num where userid=? and vrgoodsid=? ;");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
							pstmt.setInt(2, vrgoodsid);
							rs=pstmt.executeQuery();
							if(rs.next())
							{
								manowtotalrmb=rs.getString("num");
							}
							
							//增加现货瓶数变动记录
							DbConnectionManager.fastcloseStmt(rs,pstmt);
							pstmt = con.prepareStatement("INSERT INTO cd_sys_rmb (userid,payuserid,rmb,level,type,time,name,image,nowrmb,orderid,vrgoodsid) VALUES (?,?,?,?,?,?,?,?,?,?,?)");
							pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
							pstmt.setInt(2, userid);
							pstmt.setBigDecimal(3, CacheClass.toBigDecimal(vrgoodsnum+""));
							pstmt.setInt(4, level);
							pstmt.setInt(5, SysRmbRecTypeEnum.type109.getIntVlue());
							pstmt.setLong(6, now.getTime());
							pstmt.setString(7, authTmp.username);
							pstmt.setString(8, authTmp.avatar);
							pstmt.setBigDecimal(9, CacheClass.toBigDecimal(manowtotalrmb));
							pstmt.setLong(10, CacheClass.getLongValue(orderid));
							pstmt.setInt(11, vrgoodsid);
							pstmt.execute();
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutNum(CacheClass.getIntValue(tmp.pid2), level, payUsername, projectname, vrgoodsnum);
							//更新缓存
							CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid2);
						}
						
						
					}

				}
				
				
				//宝箱分成
				if(goodscut!=null && CacheClass.getIntValue(goodscut.redbag)!=0)   //有分成设置并且宝箱分成启用
				{
					
					//增加本人的正常购物分成宝箱
					int yesornot=CacheClass.getIntValue(goodscut.buy_self_redbag.trim());
					if(yesornot==1)//是否计算触发人(即本级)。　1计算，0不计算
					{
						//先本人自己增加
						//if(userid>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(userid)){
						if(userid>0){
							viplevel=userViplevel;
							level=0;
							//获得分成宝箱
							int redbagid=cutRmb.getBuyCutRedbag(level,viplevel,goodscut);
							if(redbagid>0)
							{
								//取宝箱有效天数，并计算出有效截日期
								int redbagdays=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("redbagdays").trim());
								int endtime=CacheClass.getDateAfterDay2(now, redbagdays); //yyyymmdd
								//增加用户宝箱
								for(int i=0;i<goodsnum;i++)//1件一个宝箱
								{
									DbConnectionManager.fastcloseStmt(rs,pstmt);
									pstmt = con.prepareStatement("INSERT into cd_user_redbag(touserid,buyuserid,buyusername,redbagid,level,orderid,state,createtime,endtime) VALUES(?,?,?,?,?,?,?,?,?);");
									pstmt.setInt(1, userid);
									pstmt.setInt(2, userid);
									pstmt.setString(3, payUsername);
									pstmt.setInt(4, redbagid);
									pstmt.setInt(5, level);
									pstmt.setLong(6, CacheClass.getLongValue(orderid));
									pstmt.setInt(7, 0);//0没打开过
									pstmt.setLong(8, time);
									pstmt.setInt(9, endtime);
									pstmt.execute();	
								}

								//触发，系统消息发给收获人
								pushToAPPTemplate.NewMess_BuyCutRedbag(userid, level, payUsername, projectname);
								
							}
						}	
					}
					
					//增加上一级的正常购物分成积分
					if(CacheClass.getIntValue(tmp.pid0)>0 ){
					//if(CacheClass.getIntValue(tmp.pid0)>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid0))){
						CrowdUserAuthClass authCut=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid0));
						viplevel=CacheClass.getIntValue(authCut.viplevel);
						level=1;
						
						//获得分成宝箱
						int redbagid=cutRmb.getBuyCutRedbag(level,viplevel,goodscut);
						if(redbagid>0)
						{
							//取宝箱有效天数，并计算出有效截日期
							int redbagdays=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("redbagdays").trim());
							int endtime=CacheClass.getDateAfterDay2(now, redbagdays); //yyyymmdd
							//增加用户宝箱
							for(int i=0;i<goodsnum;i++)//1件一个宝箱
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("INSERT into cd_user_redbag(touserid,buyuserid,buyusername,redbagid,level,orderid,state,createtime,endtime) VALUES(?,?,?,?,?,?,?,?,?);");
								pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
								pstmt.setInt(2, userid);
								pstmt.setString(3, payUsername);
								pstmt.setInt(4, redbagid);
								pstmt.setInt(5, level);
								pstmt.setLong(6, CacheClass.getLongValue(orderid));
								pstmt.setInt(7, 0);//0没打开过
								pstmt.setLong(8, time);
								pstmt.setInt(9, endtime);
								pstmt.execute();		
							}
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutRedbag(CacheClass.getIntValue(tmp.pid0), level, payUsername, projectname);
						}
					}
					//增加上二级的正常购物分成积分
					if(CacheClass.getIntValue(tmp.pid1)>0 ){
					//if(CacheClass.getIntValue(tmp.pid1)>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid1))){
						CrowdUserAuthClass authCut=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid1));
						viplevel=CacheClass.getIntValue(authCut.viplevel);
						level=2;
						
						//获得分成宝箱
						int redbagid=cutRmb.getBuyCutRedbag(level,viplevel,goodscut);
						if(redbagid>0)
						{
							//取宝箱有效天数，并计算出有效截日期
							int redbagdays=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("redbagdays").trim());
							int endtime=CacheClass.getDateAfterDay2(now, redbagdays); //yyyymmdd
							//增加用户宝箱
							for(int i=0;i<goodsnum;i++)//1件一个宝箱
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("INSERT into cd_user_redbag(touserid,buyuserid,buyusername,redbagid,level,orderid,state,createtime,endtime) VALUES(?,?,?,?,?,?,?,?,?);");
								pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
								pstmt.setInt(2, userid);
								pstmt.setString(3, payUsername);
								pstmt.setInt(4, redbagid);
								pstmt.setInt(5, level);
								pstmt.setLong(6, CacheClass.getLongValue(orderid));
								pstmt.setInt(7, 0);//0没打开过
								pstmt.setLong(8, time);
								pstmt.setInt(9, endtime);
								pstmt.execute();		
							}
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutRedbag(CacheClass.getIntValue(tmp.pid1), level, payUsername, projectname);
						}
					}
					
					//增加上三级的正常购物分成积分
					if(CacheClass.getIntValue(tmp.pid2)>0 ){
					//if(CacheClass.getIntValue(tmp.pid2)>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid2))){
						CrowdUserAuthClass authCut=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid2));
						viplevel=CacheClass.getIntValue(authCut.viplevel);
						level=3;
						
						//获得分成宝箱
						int redbagid=cutRmb.getBuyCutRedbag(level,viplevel,goodscut);
						if(redbagid>0)
						{
							//取宝箱有效天数，并计算出有效截日期
							int redbagdays=CacheClass.getIntValue(CacheClass.getSYS_BaseDataCacheManager().Load_Param("redbagdays").trim());
							int endtime=CacheClass.getDateAfterDay2(now, redbagdays); //yyyymmdd
							//增加用户宝箱
							for(int i=0;i<goodsnum;i++)//1件一个宝箱
							{
								DbConnectionManager.fastcloseStmt(rs,pstmt);
								pstmt = con.prepareStatement("INSERT into cd_user_redbag(touserid,buyuserid,buyusername,redbagid,level,orderid,state,createtime,endtime) VALUES(?,?,?,?,?,?,?,?,?);");
								pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
								pstmt.setInt(2, userid);
								pstmt.setString(3, payUsername);
								pstmt.setInt(4, redbagid);
								pstmt.setInt(5, level);
								pstmt.setLong(6, CacheClass.getLongValue(orderid));
								pstmt.setInt(7, 0);//0没打开过
								pstmt.setLong(8, time);
								pstmt.setInt(9, endtime);
								pstmt.execute();		
							}
							//触发，系统消息发给收获人
							pushToAPPTemplate.NewMess_BuyCutRedbag(CacheClass.getIntValue(tmp.pid2), level, payUsername, projectname);
						}
					}

				}
								
				
				
				
				
				//增加分公司的收益
				if(CacheClass.isEmpty(authTmp.iscompanyid) || CacheClass.getIntValue(authTmp.iscompanyid)==0)
				{
					//没有分公司，不处理
				}
				else //
				{
					//取分公司的父公司
					CrowdSmallCompanyClass obj=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(authTmp.iscompanyid);
					CrowdSmallCompanyClass obj0=null;
					CrowdSmallCompanyClass obj1=null;
					if(obj!=null)
					{
						if(CacheClass.getIntValue(obj.pid0)!=0)
							obj0=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(obj.pid0);
						if(CacheClass.getIntValue(obj.pid1)!=0)
							obj1=CrowdSmallCompanyCacheManager.getInstance().loadSmallCompanyByid(obj.pid1);
						
					}
					int ymd=CacheClass.getYMD_int();
					int hms=CacheClass.getHms_int();
					//先加本办事处
					if(obj!=null)
					{
						int regular_rate=CacheClass.getIntValue(obj.regular_rate);
						if(regular_rate<0) regular_rate=0;
						
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_sys_smallcompany set totalrmb=(totalrmb+?) where smallcompanyid=?");
						pstmt.setBigDecimal(1, CacheClass.Bdiv_D(CacheClass.Bmul(regular_rate+"", totalRmb+""), "1000", 2));
						pstmt.setInt(2, CacheClass.getIntValue(obj.smallcompanyid));
						pstmt.executeUpdate();
						
						//增加分公司记录
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("insert into cd_sys_smallcompanylog " +
								" (smallcompanyid,ymd,hms,buyrmb,buynum,cutrate,cutrmb,orderid,userid,about) " +
								" values " +
								" (?,?,?,?,?,?,?,?,?,?);");
			
						pstmt.setInt(1, CacheClass.getIntValue(obj.smallcompanyid));
						pstmt.setInt(2, ymd);
						pstmt.setInt(3, hms);
						pstmt.setFloat(4, totalRmb);
						pstmt.setInt(5, goodsnum);
						pstmt.setInt(6, regular_rate);
						pstmt.setBigDecimal(7, CacheClass.Bdiv_D(CacheClass.Bmul(regular_rate+"", totalRmb+""), "1000", 2));
						pstmt.setInt(8, CacheClass.getIntValue(orderid));
						pstmt.setInt(9, userid);
						pstmt.setString(10, obj.smallcompanyname);
						pstmt.execute();
						
					}
					
					
					
					//上一级办事处
					if(obj0!=null)
					{
						int regular_rate=CacheClass.getIntValue(obj0.regular_rate);
						if(obj!=null)
						{
							regular_rate=regular_rate-CacheClass.getIntValue(obj.regular_rate);
						}
						if(regular_rate<0) regular_rate=0;
						
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_sys_smallcompany set totalrmb=(totalrmb+?) where smallcompanyid=?");
						pstmt.setBigDecimal(1, CacheClass.Bdiv_D(CacheClass.Bmul(regular_rate+"", totalRmb+""), "1000", 2));
						pstmt.setInt(2, CacheClass.getIntValue(obj0.smallcompanyid));
						pstmt.executeUpdate();
						
						//增加分公司记录
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("insert into cd_sys_smallcompanylog " +
								" (smallcompanyid,ymd,hms,buyrmb,buynum,cutrate,cutrmb,orderid,userid,about) " +
								" values " +
								" (?,?,?,?,?,?,?,?,?,?);");
						
						pstmt.setInt(1, CacheClass.getIntValue(obj0.smallcompanyid));
						pstmt.setInt(2, ymd);
						pstmt.setInt(3, hms);
						pstmt.setFloat(4, totalRmb);
						pstmt.setInt(5, goodsnum);
						pstmt.setInt(6, regular_rate);
						pstmt.setBigDecimal(7, CacheClass.Bdiv_D(CacheClass.Bmul(regular_rate+"", totalRmb+""), "1000", 2));
						pstmt.setInt(8, CacheClass.getIntValue(orderid));
						pstmt.setInt(9, userid);
						pstmt.setString(10, obj.smallcompanyname+"->"+obj0.smallcompanyname);
						pstmt.execute();
						
					}
					//上二级办事处
					if(obj1!=null)
					{
						int regular_rate=CacheClass.getIntValue(obj1.regular_rate);
						if(obj0!=null)
						{
							regular_rate=regular_rate-CacheClass.getIntValue(obj0.regular_rate);
						}
						if(regular_rate<0) regular_rate=0;
						
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_sys_smallcompany set totalrmb=(totalrmb+?) where smallcompanyid=?");
						pstmt.setBigDecimal(1, CacheClass.Bdiv_D(CacheClass.Bmul(regular_rate+"", totalRmb+""), "1000", 2));
						pstmt.setInt(2, CacheClass.getIntValue(obj1.smallcompanyid));
						pstmt.executeUpdate();
						
						//增加分公司记录
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("insert into cd_sys_smallcompanylog " +
								" (smallcompanyid,ymd,hms,buyrmb,buynum,cutrate,cutrmb,orderid,userid,about) " +
								" values " +
								" (?,?,?,?,?,?,?,?,?,?);");
						pstmt.setInt(1, CacheClass.getIntValue(obj1.smallcompanyid));
						pstmt.setInt(2, ymd);
						pstmt.setInt(3, hms);
						pstmt.setFloat(4, totalRmb);
						pstmt.setInt(5, goodsnum);
						pstmt.setInt(6, regular_rate);
						pstmt.setBigDecimal(7, CacheClass.Bdiv_D(CacheClass.Bmul(regular_rate+"", totalRmb+""), "1000", 2));
						pstmt.setInt(8, CacheClass.getIntValue(orderid));
						pstmt.setInt(9, userid);
						pstmt.setString(10, obj.smallcompanyname+"->"+obj0.smallcompanyname+"->"+obj1.smallcompanyname);
						pstmt.execute();
						
					}
					
					
				}
				
		
			} catch (Exception e) {
				authTmp=null;
				e.printStackTrace();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}
		
		return authTmp;
		
	}


	//&新鲜宝  注册 活跃  int type=0; //0:注册　1购买　2登陆签到
	public CrowdUserAuthClass InvitePreCut(int userid,int type,float totalrmb,int orderid)
	{
		
		CrowdUserParentClass tmp=user_parentCacheManager.getInstance().load_user_Parent(userid);
		if(tmp==null) return null;


		CrowdUserAuthClass authTmp=null;
		
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		long time=CacheClass.getTime();
		
		authTmp=CrowdUserAuthCache.getInstance().getUser(userid);
		String payUsername=authTmp.username;
		if(type==2) //2登陆签到
		{
			//当天是否已签到过了?
			if(CacheClass.getIntValue(authTmp.signtime)==CacheClass.getDate())
			{
				return null;
			}
			
		}
		
			try {
				con = DbConnectionManager.getConnection();
				
				//增加注册本人的 活跃
				CrowdUserAuthClass authObj=CrowdUserAuthCache.getInstance().getUser(userid);
				CrowdPrestigeParmDataClass parm=CrowdPrestigeParmCache.getInstance().Load_CrowdPrestigeParm(type,CacheClass.getIntValue(authObj.level));
				int regular_num0=0;
				if(parm!=null){
					regular_num0=CacheClass.getIntValue(parm.regular_num0.trim());
				}
				if(type==1)
				{ 
					regular_num0=(int) Math.round((regular_num0*totalrmb)/100.00);
				}
				if(regular_num0>0)//是否计算触发人(即本级)的邀请积分。　1计算，0不计算
				{
					//先本人自己增加
					//增加活跃
					DbConnectionManager.fastcloseStmt(rs,pstmt);
					pstmt = con.prepareStatement("update cd_user_rmb set prestigevalue=(prestigevalue+?) where userid=?");
					pstmt.setInt(1, regular_num0);
					pstmt.setInt(2, userid);
					pstmt.executeUpdate();
					
					//取更新后的新值
					int newlogvalue=0;
					DbConnectionManager.fastcloseStmt(rs,pstmt);
					pstmt = con.prepareStatement("select prestigevalue from cd_user_rmb where userid=?");
					pstmt.setInt(1, userid);
					rs=pstmt.executeQuery();
					if(rs.next())
					{
						newlogvalue=rs.getInt("prestigevalue");
					}
                    //记录
					pstmt = con.prepareStatement("INSERT INTO cd_sys_prestigelog (userid,payuserid,type,logvalue,newlogvalue,ctime,orderid) VALUES (?,?,?,?,?,?,?)");
					pstmt.setInt(1, userid);
					pstmt.setInt(2, userid);
					pstmt.setFloat(3, type);
					pstmt.setInt(4, regular_num0);
					pstmt.setInt(5, newlogvalue);
					pstmt.setLong(6, time);
					if (orderid!=0) {
						pstmt.setInt(7, orderid);
					} else {
						pstmt.setNull(7, Types.VARCHAR);
					}
					pstmt.execute();	

					//触发，系统消息发给收获人
					pushToAPPTemplate.NewMess_InvitePreCut(userid,payUsername, regular_num0,type);
					
					//更新缓存
					CrowdUserAuthCache.getInstance().ClearObjectCache(userid+"");
					
				}
				
				//增加上一级的注册　活跃
				if(CacheClass.getIntValue(tmp.pid0)>0){
					authObj=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid0));
					parm=CrowdPrestigeParmCache.getInstance().Load_CrowdPrestigeParm(type,CacheClass.getIntValue(authObj.level));
					int regular_num1=0;
					if(parm!=null){
						regular_num1=CacheClass.getIntValue(parm.regular_num1.trim());
					}
					if(type==1)//购物
					{ 
						regular_num1=(int) Math.round((regular_num1*totalrmb)/100.00);
					}					
					if(regular_num1>0)
					{
						//if(CacheClass.getIntValue(tmp.pid0)>0 && regular_num1>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid0))){
						//增加活跃
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_user_rmb set prestigevalue=(prestigevalue+?) where userid=?");
						pstmt.setInt(1, regular_num1);
						pstmt.setInt(2, CacheClass.getIntValue(tmp.pid0));
						pstmt.executeUpdate();
						//取更新后的新值
						int newlogvalue=0;
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("select prestigevalue from cd_user_rmb where userid=?");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
						rs=pstmt.executeQuery();
						if(rs.next())
						{
							newlogvalue=rs.getInt("prestigevalue");
						}
						//记录
						pstmt = con.prepareStatement("INSERT INTO cd_sys_prestigelog (userid,payuserid,type,logvalue,newlogvalue,ctime,orderid) VALUES (?,?,?,?,?,?,?)");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid0));
						pstmt.setInt(2, userid);
						pstmt.setFloat(3, type);
						pstmt.setInt(4, regular_num1);
						pstmt.setInt(5, newlogvalue);
						pstmt.setLong(6, time);
						if (orderid!=0) {
							pstmt.setInt(7, orderid);
						} else {
							pstmt.setNull(7, Types.VARCHAR);
						}
						pstmt.execute();
						
						//触发，系统消息发给收获人
						pushToAPPTemplate.NewMess_InvitePreCut(CacheClass.getIntValue(tmp.pid0),  payUsername, regular_num1,type);
						
						//更新缓存
						CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid0);
					}
				}
				//增加上二级的注册　活跃
				if(CacheClass.getIntValue(tmp.pid1)>0){
					authObj=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid1));
					parm=CrowdPrestigeParmCache.getInstance().Load_CrowdPrestigeParm(type,CacheClass.getIntValue(authObj.level));
					int regular_num2=0;
					if(parm!=null){
						regular_num2=CacheClass.getIntValue(parm.regular_num2.trim());
					}
					if(type==1)//购物
					{ 
						regular_num2=(int) Math.round((regular_num2*totalrmb)/100.00);
					}
					if(regular_num2>0 )
					{
						//if(CacheClass.getIntValue(tmp.pid1)>0 && regular_num2>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid1))){
						//增加活跃
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_user_rmb set prestigevalue=(prestigevalue+?) where userid=?");
						pstmt.setInt(1, regular_num2);
						pstmt.setInt(2, CacheClass.getIntValue(tmp.pid1));
						pstmt.executeUpdate();
						//取更新后的新值
						int newlogvalue=0;
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("select prestigevalue from cd_user_rmb where userid=?");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
						rs=pstmt.executeQuery();
						if(rs.next())
						{
							newlogvalue=rs.getInt("prestigevalue");
						}
						//记录
						pstmt = con.prepareStatement("INSERT INTO cd_sys_prestigelog (userid,payuserid,type,logvalue,newlogvalue,ctime,orderid) VALUES (?,?,?,?,?,?,?)");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid1));
						pstmt.setInt(2, userid);
						pstmt.setFloat(3, type);
						pstmt.setInt(4, regular_num2);
						pstmt.setInt(5, newlogvalue);
						pstmt.setLong(6, time);
						pstmt.setLong(6, time);
						if (orderid!=0) {
							pstmt.setInt(7, orderid);
						} else {
							pstmt.setNull(7, Types.VARCHAR);
						}
						pstmt.execute();
	 	

						//触发，系统消息发给收获人
						pushToAPPTemplate.NewMess_InvitePreCut(CacheClass.getIntValue(tmp.pid1),  payUsername, regular_num2,type);
						
						//更新缓存
						CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid1);
					}
				}
				
				//增加上三级的注册　活跃
				if(CacheClass.getIntValue(tmp.pid2)>0){
					authObj=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid2));
					parm=CrowdPrestigeParmCache.getInstance().Load_CrowdPrestigeParm(type,CacheClass.getIntValue(authObj.level));
					int regular_num3=0;
					if(parm!=null){
						regular_num3=CacheClass.getIntValue(parm.regular_num3.trim());
					}
					if(type==1)//购物
					{ 
						regular_num3=(int) Math.round((regular_num3*totalrmb)/100.00);
					}
					if(regular_num3>0)
					{
						//if(CacheClass.getIntValue(tmp.pid2)>0 && regular_num3>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid2))){
						//增加活跃
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_user_rmb set prestigevalue=(prestigevalue+?) where userid=?");
						pstmt.setInt(1, regular_num3);
						pstmt.setInt(2, CacheClass.getIntValue(tmp.pid2));
						pstmt.executeUpdate();
						//取更新后的新值
						int newlogvalue=0;
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("select prestigevalue from cd_user_rmb where userid=?");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
						rs=pstmt.executeQuery();
						if(rs.next())
						{
							newlogvalue=rs.getInt("prestigevalue");
						}
						//记录
						pstmt = con.prepareStatement("INSERT INTO cd_sys_prestigelog (userid,payuserid,type,logvalue,newlogvalue,ctime,orderid) VALUES (?,?,?,?,?,?,?)");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid2));
						pstmt.setInt(2, userid);
						pstmt.setFloat(3, type);
						pstmt.setInt(4, regular_num3);
						pstmt.setInt(5, newlogvalue);
						pstmt.setLong(6, time);
						if (orderid!=0) {
							pstmt.setInt(7, orderid);
						} else {
							pstmt.setNull(7, Types.VARCHAR);
						}
						pstmt.execute();

						//触发，系统消息发给收获人
						pushToAPPTemplate.NewMess_InvitePreCut(CacheClass.getIntValue(tmp.pid2),  payUsername, regular_num3,type);
						
						//更新缓存
						CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid2);
					}
				}
				//增加上四级的注册　活跃
				if(CacheClass.getIntValue(tmp.pid3)>0){
					authObj=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid3));
					parm=CrowdPrestigeParmCache.getInstance().Load_CrowdPrestigeParm(type,CacheClass.getIntValue(authObj.level));
					int regular_num4=0;
					if(parm!=null){
						regular_num4=CacheClass.getIntValue(parm.regular_num4.trim());
					}
					if(type==1)//购物
					{ 
						regular_num4=(int) Math.round((regular_num4*totalrmb)/100.00);
					}
					if(regular_num4>0)
					{
						//if(CacheClass.getIntValue(tmp.pid3)>0 && regular_num4>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid3))){
						//增加活跃
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_user_rmb set prestigevalue=(prestigevalue+?) where userid=?");
						pstmt.setInt(1, regular_num4);
						pstmt.setInt(2, CacheClass.getIntValue(tmp.pid3));
						pstmt.executeUpdate();
						//取更新后的新值
						int newlogvalue=0;
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("select prestigevalue from cd_user_rmb where userid=?");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid3));
						rs=pstmt.executeQuery();
						if(rs.next())
						{
							newlogvalue=rs.getInt("prestigevalue");
						}
						//记录
						pstmt = con.prepareStatement("INSERT INTO cd_sys_prestigelog (userid,payuserid,type,logvalue,newlogvalue,ctime,orderid) VALUES (?,?,?,?,?,?,?)");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid3));
						pstmt.setInt(2, userid);
						pstmt.setFloat(3, type);
						pstmt.setInt(4, regular_num4);
						pstmt.setInt(5, newlogvalue);
						pstmt.setLong(6, time);
						if (orderid!=0) {
							pstmt.setInt(7, orderid);
						} else {
							pstmt.setNull(7, Types.VARCHAR);
						}
						pstmt.execute();

						//触发，系统消息发给收获人
						pushToAPPTemplate.NewMess_InvitePreCut(CacheClass.getIntValue(tmp.pid3),  payUsername, regular_num4,type);
						
						//更新缓存
						CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid3);
					}
				}
				//增加上五级的注册　活跃
				if(CacheClass.getIntValue(tmp.pid4)>0){
					authObj=CrowdUserAuthCache.getInstance().getUser(CacheClass.getIntValue(tmp.pid4));
					parm=CrowdPrestigeParmCache.getInstance().Load_CrowdPrestigeParm(type,CacheClass.getIntValue(authObj.level));
					int regular_num5=0;
					if(parm!=null){
						regular_num5=CacheClass.getIntValue(parm.regular_num5.trim());
					}
					if(type==1)//购物
					{ 
						regular_num5=(int) Math.round((regular_num5*totalrmb)/100.00);
					}
					if(regular_num5>0)
					{
						//if(CacheClass.getIntValue(tmp.pid4)>0 && regular_num5>0 && !CrowdSmallCompanyCacheManager.getInstance().isSmallCompanyByUserid(CacheClass.getIntValue(tmp.pid4))){
						//增加活跃
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("update cd_user_rmb set prestigevalue=(prestigevalue+?) where userid=?");
						pstmt.setInt(1, regular_num5);
						pstmt.setInt(2, CacheClass.getIntValue(tmp.pid4));
						pstmt.executeUpdate();
						//取更新后的新值
						int newlogvalue=0;
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						pstmt = con.prepareStatement("select prestigevalue from cd_user_rmb where userid=?");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid4));
						rs=pstmt.executeQuery();
						if(rs.next())
						{
							newlogvalue=rs.getInt("prestigevalue");
						}
						//记录
						pstmt = con.prepareStatement("INSERT INTO cd_sys_prestigelog (userid,payuserid,type,logvalue,newlogvalue,ctime,orderid) VALUES (?,?,?,?,?,?,?)");
						pstmt.setInt(1, CacheClass.getIntValue(tmp.pid4));
						pstmt.setInt(2, userid);
						pstmt.setFloat(3, type);
						pstmt.setInt(4, regular_num5);
						pstmt.setInt(5, newlogvalue);
						pstmt.setLong(6, time);
						if (orderid!=0) {
							pstmt.setInt(7, orderid);
						} else {
							pstmt.setNull(7, Types.VARCHAR);
						}
						pstmt.execute();
	 

						//触发，系统消息发给收获人
						pushToAPPTemplate.NewMess_InvitePreCut(CacheClass.getIntValue(tmp.pid4),  payUsername, regular_num5,type);
						
						//更新缓存
						CrowdUserAuthCache.getInstance().ClearObjectCache(tmp.pid4);
					}
				
				}

				
				if(type==2) //2登陆签到
				{
					//更新签到日期
					DbConnectionManager.fastcloseStmt(rs,pstmt);
					pstmt = con.prepareStatement("update cd_user_auth set signtime=? where userid=?");
					pstmt.setInt(1, CacheClass.getDate());
					pstmt.setInt(2, userid);
					pstmt.executeUpdate();
				}
				
			} catch (Exception e) {
				authTmp=null;
				e.printStackTrace();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}
		
		return authTmp;
		
	}	
	


	//众筹  排行榜
	public PublicData getRankInfo(int type,int curpage)
	{
		 Connection con = null;
	     PreparedStatement pstmt = null;
	     ResultSet rs = null;
	     PublicData tmp=null;
	     ArrayList<Object> listArry= null;
	     try {
	    	
	        List<CrowdUserAuthClass> authObjList= new ArrayList<CrowdUserAuthClass>();
	        int  total=0;

	        con = DbConnectionManager.getConnection();
    	    pstmt = con.prepareCall(Load_RankInfo);  		 
            pstmt.setInt(1, type);
            pstmt.setInt(2, curpage);

            boolean hadResults=pstmt.execute();
            int mm=0;
            while (hadResults) {
            	if(tmp==null) tmp=new PublicData();
               	rs=pstmt.getResultSet();
               	if(mm==0)
               	{
               		while(rs != null && rs.next())
               		{
               			if(type==0){
               				CrowdUserAuthClass Objtmp=new CrowdUserAuthClass();
	               			Objtmp.userid=rs.getString("userid");
	               			Objtmp.username=rs.getString("username");
	               			Objtmp.avatar=rs.getString("avatar");
	               			Objtmp.rank=rs.getString("rank");
	               			Objtmp.totalbuyscore=rs.getString("totalbuyscore");
	               			Objtmp.totalinvestrmb=rs.getString("totalinvestrmb");
	               			authObjList.add(Objtmp);
               			}
               			else if(type==1)
               			{
               				
               				CrowdUserAuthClass Objtmp=new CrowdUserAuthClass();
	               			Objtmp.userid=rs.getString("userid");
	               			Objtmp.username=rs.getString("username");
	               			Objtmp.avatar=rs.getString("avatar");
	               			Objtmp.rank=rs.getString("rank");
	               			Objtmp.prestigevalue=rs.getString("prestigevalue");
	               			authObjList.add(Objtmp);
               			}

               		}
               		
               	}
              	else if(mm==1)
               	{
               		while(rs != null && rs.next()){
               			total=total+rs.getInt(1);
               		}
               	}
                hadResults = pstmt.getMoreResults(); //检查是否存在更多结果集
                mm++;
            }
        

 	       if(authObjList.size()>0)
 	       {
 	    	   if(listArry==null) listArry=new ArrayList<Object>();
 	    	   listArry.addAll(authObjList);
 	       }
   			if((total%20)>0)
   			{
   				tmp.total=(total/20+1)+"";
   			}
   			else
   				tmp.total=(total/20)+"";

            tmp.curpage=curpage+"";
            tmp.list=listArry;

       }
       catch (Exception e) {
       		tmp=null;
           e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	    return tmp;

	}
	
	//生成预转出订单
	public String Create_User_Preorder_VrtoCr(int userid) throws LzPluginException
	{
		boolean abortTransaction = false;
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		Date now = new Date();
		String cdvrid=null;
		try {
			//众筹平台减少  入金次数增加
			con = DbConnectionManager.getTransactionConnection();
			pstmt = con.prepareStatement("INSERT INTO cdvr_sys_rmb (userid,type,rmb,rmb0type,vrcashgiftrmb1,vrcashgiftrmb2,rmbtype1,state,time) VALUES (?,?,?,?,?,?,?,?,?)");
			pstmt.setInt(1, userid);
			pstmt.setInt(2, 1); //0:入金　1:出金
			pstmt.setBigDecimal(3,CacheClass.getBigDecimal(0+""));
			pstmt.setInt(4, 1); //转入金性质  1抵押金  0普通余额
			pstmt.setBigDecimal(5,CacheClass.getBigDecimal(0+""));
			pstmt.setBigDecimal(6,CacheClass.getBigDecimal(0+""));
			pstmt.setInt(7, 1); //赠券性质  1可损  0不可损
			pstmt.setInt(8, 0);//0:冻结 1成功 2失败
			pstmt.setLong(9, now.getTime());
			pstmt.execute();
			
			//出得刚增加的出入金订单id
			DbConnectionManager.fastcloseStmt(rs,pstmt);
			pstmt = con.prepareStatement("select * from cdvr_sys_rmb where userid=? and time=? and state=? ");
			pstmt.setInt(1, userid);
			pstmt.setLong(2, now.getTime()); 
			pstmt.setInt(3, 0);//0:冻结
			rs=pstmt.executeQuery();
			if(rs.next())
			{
				cdvrid=rs.getString("cdvrid");
			}

		} catch (Exception e) {
			abortTransaction = true;
			cdvrid=null;
			e.printStackTrace();
			if(e instanceof LzPluginException)
			{
				throw new LzPluginException(e.getMessage());
			}
			
		} finally {
			DbConnectionManager.closeTransactionConnection(rs,pstmt, con,abortTransaction);
		}

		return cdvrid;
	}		
	
	//从理财平台 转出资金到 众筹平台  失败
	public void Update_User_Rmb_toCrowd2(long cdvrid) 
	{
		Connection con = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		try {
			//众筹平台减少  入金次数增加
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("update cdvr_sys_rmb set state=? where cdvrid=?");
			pstmt.setInt(1, 2);//0:冻结 1成功 2失败
			pstmt.setLong(2, cdvrid);
			pstmt.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}

	}	
	//取分公司id
	public String load_Small_comapny(int smallcompanyid) {
		
		String tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {

			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select smallcompanyid from cd_sys_smallcompany WHERE smallcompanyid=?");
			pstmt.setInt(1, smallcompanyid);
			rs=pstmt.executeQuery();
			if(rs.next())
			{
				tmp=rs.getNString("smallcompanyid");
			}
	
		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	public static void main(String[] args) {
		
		int rmb=1354771;
		float vrcash,crnowrmb;
		vrcash=49800.00f;
		crnowrmb =1354771/100f;
	    System.out.println("  "+ crnowrmb);
	    
	}
	
	
}



